{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 15.15 C\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3aPython\u5b57\u7b26\u4e32\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u600e\u6837\u5c06C\u4e2d\u7684\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3aPython\u5b57\u8282\u6216\u4e00\u4e2a\u5b57\u7b26\u4e32\u5bf9\u8c61\uff1f"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "C\u5b57\u7b26\u4e32\u4f7f\u7528\u4e00\u5bf9 char * \u548c int \u6765\u8868\u793a\uff0c\n\u4f60\u9700\u8981\u51b3\u5b9a\u5b57\u7b26\u4e32\u5230\u5e95\u662f\u7528\u4e00\u4e2a\u539f\u59cb\u5b57\u8282\u5b57\u7b26\u4e32\u8fd8\u662f\u4e00\u4e2aUnicode\u5b57\u7b26\u4e32\u6765\u8868\u793a\u3002\n\u5b57\u8282\u5bf9\u8c61\u53ef\u4ee5\u50cf\u4e0b\u9762\u8fd9\u6837\u4f7f\u7528 Py_BuildValue() \u6765\u6784\u5efa\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "char *s;     /* Pointer to C string data */\nint   len;   /* Length of data */\n\n/* Make a bytes object */\nPyObject *obj = Py_BuildValue(\"y#\", s, len);"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u8981\u521b\u5efa\u4e00\u4e2aUnicode\u5b57\u7b26\u4e32\uff0c\u5e76\u4e14\u4f60\u77e5\u9053 s \u6307\u5411\u4e86UTF-8\u7f16\u7801\u7684\u6570\u636e\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e0b\u9762\u7684\u65b9\u5f0f\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "PyObject *obj = Py_BuildValue(\"s#\", s, len);"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c s \u4f7f\u7528\u5176\u4ed6\u7f16\u7801\u65b9\u5f0f\uff0c\u90a3\u4e48\u53ef\u4ee5\u50cf\u4e0b\u9762\u4f7f\u7528 PyUnicode_Decode() \u6765\u6784\u5efa\u4e00\u4e2a\u5b57\u7b26\u4e32\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "PyObject *obj = PyUnicode_Decode(s, len, \"encoding\", \"errors\");\n\n/* Examples /*\nobj = PyUnicode_Decode(s, len, \"latin-1\", \"strict\");\nobj = PyUnicode_Decode(s, len, \"ascii\", \"ignore\");"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u6070\u597d\u6709\u4e00\u4e2a\u7528 wchar_t *, len \u5bf9\u8868\u793a\u7684\u5bbd\u5b57\u7b26\u4e32\uff0c\n\u6709\u51e0\u79cd\u9009\u62e9\u6027\u3002\u9996\u5148\u4f60\u53ef\u4ee5\u4f7f\u7528 Py_BuildValue() \uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "wchar_t *w;    /* Wide character string */\nint len;       /* Length */\n\nPyObject *obj = Py_BuildValue(\"u#\", w, len);"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u53e6\u5916\uff0c\u4f60\u8fd8\u53ef\u4ee5\u4f7f\u7528 PyUnicode_FromWideChar() :"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "PyObject *obj = PyUnicode_FromWideChar(w, len);"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5bf9\u4e8e\u5bbd\u5b57\u7b26\u4e32\uff0c\u5e76\u6ca1\u6709\u5bf9\u5b57\u7b26\u6570\u636e\u8fdb\u884c\u89e3\u6790\u2014\u2014\u5b83\u88ab\u5047\u5b9a\u662f\u539f\u59cbUnicode\u7f16\u7801\u6307\u9488\uff0c\u53ef\u4ee5\u88ab\u76f4\u63a5\u8f6c\u6362\u6210Python\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5c06C\u4e2d\u7684\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3aPython\u5b57\u7b26\u4e32\u9075\u5faa\u548cI/O\u540c\u6837\u7684\u539f\u5219\u3002\n\u4e5f\u5c31\u662f\u8bf4\uff0c\u6765\u81eaC\u4e2d\u7684\u6570\u636e\u5fc5\u987b\u6839\u636e\u4e00\u4e9b\u89e3\u7801\u5668\u88ab\u663e\u5f0f\u7684\u89e3\u7801\u4e3a\u4e00\u4e2a\u5b57\u7b26\u4e32\u3002\n\u901a\u5e38\u7f16\u7801\u683c\u5f0f\u5305\u62ecASCII\u3001Latin-1\u548cUTF-8.\n\u5982\u679c\u4f60\u5e76\u4e0d\u786e\u5b9a\u7f16\u7801\u65b9\u5f0f\u6216\u8005\u6570\u636e\u662f\u4e8c\u8fdb\u5236\u7684\uff0c\u4f60\u6700\u597d\u5c06\u5b57\u7b26\u4e32\u7f16\u7801\u6210\u5b57\u8282\u3002\n\u5f53\u6784\u9020\u4e00\u4e2a\u5bf9\u8c61\u7684\u65f6\u5019\uff0cPython\u901a\u5e38\u4f1a\u590d\u5236\u4f60\u63d0\u4f9b\u7684\u5b57\u7b26\u4e32\u6570\u636e\u3002\n\u5982\u679c\u6709\u5fc5\u8981\u7684\u8bdd\uff0c\u4f60\u9700\u8981\u5728\u540e\u9762\u53bb\u91ca\u653eC\u5b57\u7b26\u4e32\u3002\n\u540c\u65f6\uff0c\u4e3a\u4e86\u8ba9\u7a0b\u5e8f\u66f4\u52a0\u5065\u58ee\uff0c\u4f60\u5e94\u8be5\u540c\u65f6\u4f7f\u7528\u4e00\u4e2a\u6307\u9488\u548c\u4e00\u4e2a\u5927\u5c0f\u503c\uff0c\n\u800c\u4e0d\u662f\u4f9d\u8d56NULL\u7ed3\u5c3e\u6570\u636e\u6765\u521b\u5efa\u5b57\u7b26\u4e32\u3002"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}